/*
* Replicating the quantile regressions of SOFR-IOR in the Reserves & repo paper Copeland Duffie and Yang
*
* march 2024
*/

pwd
clear
// cd to the current directory where data is stored. 
cd "C:\Users\yiliyang\DropBox.davyang.alumni.stanford.edu\Dropbox\Repo_Data\PaperAndSlides\QJE-R&R SecondRound\ReplicationPackage\ReplicationCodeAndData\Table2Table14"



// Loading in the data
import delimited using master_data_March2024.csv, clear
keep sofr_ioer repo_balances1 tdiff50_recv1 quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins date tdiff25_newiv tdiff50_newiv tdiff75_newiv total_issuance_all netposition_try
replace tdiff50_recv1 = "." if tdiff50_recv1=="NA"
replace tdiff50_newiv = "." if tdiff50_newiv=="NA"
replace tdiff25_newiv = "." if tdiff25_newiv=="NA"
replace tdiff75_newiv = "." if tdiff75_newiv=="NA"
replace repo_balances1 = "." if repo_balances1=="NA"  
replace total_redemptions_all = "." if total_redemptions_all=="NA"  
replace total_issuance_bills = "." if total_issuance_bills=="NA"  
replace total_issuance_all = "." if total_issuance_all=="NA"    
replace coupon_issuances = "." if coupon_issuances=="NA"  
replace tbills_outstanding = "." if tbills_outstanding=="NA" 
replace corporation_income_taxes_trillio = "." if corporation_income_taxes_trillio=="NA"  
replace netposition_try = "." if netposition_try=="NA" 

destring , replace

// IV quantile regression, main Table 2 Column 2
ivqregress smooth sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv corporation_income_taxes_trillio), quantile(99)  vce(bootstrap)

// Caution: The standard error is generated by bootstrap, so every run may generate some different numbers. The range is about 20-23 for the main variable repo_balances1. 
// -------Results (Sept 2024 run):----------------
// IV .99 quantile regression                              Number of obs =  2,039
// Estimator: Smoothed estimating equations                Replications  =     43
//                                                         Wald chi2(8)  = 230.96
//                                                         Prob > chi2   = 0.0000
//
// --------------------------------------------------------------------------------------------------
//                                  |   Observed   Bootstrap                         Normal-based
//                        sofr_ioer | coefficient  std. err.      z    P>|z|     [95% conf. interval]
// ---------------------------------+----------------------------------------------------------------
//                   repo_balances1 |  -74.11317   21.08145    -3.52   0.000    -115.4321   -32.79428
//                       quarterend |   31.45181   6.614921     4.75   0.000     18.48681    44.41682
//               tbills_outstanding |   3.220775   1.472223     2.19   0.029     .3352709    6.106279
//            total_redemptions_all |  -13.65459   23.48611    -0.58   0.561    -59.68651    32.37733
//             total_issuance_bills |   23.18326   26.03801     0.89   0.373     -27.8503    74.21682
//                 coupon_issuances |   38.81369   25.97658     1.49   0.135    -12.09946    89.72685
// corporation_income_taxes_trillio |   15.24422   118.5939     0.13   0.898    -217.1956    247.6841
//                      dlrdepunins |   24.14342   11.72509     2.06   0.039     1.162657    47.12418
//                            _cons |   6.338681   27.59167     0.23   0.818    -47.73999    60.41735
// --------------------------------------------------------------------------------------------------
// Endogenous: repo_balances1
// Exogenous:  quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins tdiff25_newiv tdiff50_newiv
//             tdiff75_newiv
//
// . 
// end of do-file
// -------Results (June 2023 run, used in the paper):----------------
//
// IV .99 quantile regression                              Number of obs =  2,039
// Estimator: Smoothed estimating equations                Replications  =     41
//                                                         Wald chi2(8)  = 248.56
//                                                         Prob > chi2   = 0.0000
//
// --------------------------------------------------------------------------------------------------
//                                  |   Observed   Bootstrap                         Normal-based
//                        sofr_ioer | coefficient  std. err.      z    P>|z|     [95% conf. interval]
// ---------------------------------+----------------------------------------------------------------
//                   repo_balances1 |  -74.11316   20.48123    -3.62   0.000    -114.2556   -33.97069
//                       quarterend |   31.45182   6.681139     4.71   0.000     18.35702    44.54661
//               tbills_outstanding |   3.220775   1.382571     2.33   0.020     .5109856    5.930564
//            total_redemptions_all |  -13.65459   24.24826    -0.56   0.573     -61.1803    33.87112
//             total_issuance_bills |   23.18326   27.05591     0.86   0.392    -29.84536    76.21188
//                 coupon_issuances |   38.81372   26.19635     1.48   0.138    -12.53018    90.15761
// corporation_income_taxes_trillio |   15.24422    120.439     0.13   0.899    -220.8118    251.3003
//                      dlrdepunins |   24.14341   11.33286     2.13   0.033     1.931413    46.35541
//                            _cons |   6.338683   21.67916     0.29   0.770    -36.15169    48.82906
// --------------------------------------------------------------------------------------------------
// Endogenous: repo_balances1
// Exogenous:  quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins tdiff25_newiv tdiff50_newiv tdiff75_newiv
// -------End----------------


// Data for Figure 11--------------------------------
ivqregress smooth sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv corporation_income_taxes_trillio), quantile(51	 	53	 	55	 	57	 	59	 	61	 	63	 	65	 	67	 	69	 	71	 	73	 	75	 	77	 	79   81	 	83	 	85	 		 	91	 	93	 	95	 	97	 	99) 
//87 and 89 do not converge:

ivqregress smooth sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv corporation_income_taxes_trillio), quantile(87) 

ivqregress smooth sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv corporation_income_taxes_trillio), quantile(89) 

// Command for Table 14--------------------------------
ivqregress iqr sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv), bound(-300 300) ngrid(1100)  quantile(99)  vce(robust)

ivqregress iqr sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv), bound(-300 300) ngrid(900)  quantile(99)  vce(robust)

ivqregress iqr sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv), bound(-300 300) ngrid(700)  quantile(99)  vce(robust)

ivqregress iqr sofr_ioer quarterend tbills_outstanding total_redemptions_all total_issuance_bills coupon_issuances corporation_income_taxes_trillio dlrdepunins (repo_balances1 = tdiff25_newiv tdiff50_newiv tdiff75_newiv), bound(-300 300) ngrid(500)  quantile(99)  vce(robust)



